<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\Account;

use  AuctionCore\System\AuctionException;
use \AuctionCore\Common\PhpMemcache;

/**
* 前台帐号功能类
* @author  爱民
*/
class FrontUser extends \AuctionCore\Account\User {

    public $user_name;

	/**
    * 构造,初始化前台auth对象
    * @param    String $user_name    前台登录用户名
    * @return   void
    */
    public function __construct($account_name){
        $f_auth = new \AuctionCore\Account\FrontAuth();            
        $this->auth_object = $f_auth;        
        parent::__construct($account_name);
    }

    /**
    * 创建网站用户
    * @param array info [用户名，密码,昵称,密保手机号,短信手机号]
    * @return user_id
    */
    public static function create($info,$is_md5=false){
        try{
            $f_auth = new \AuctionCore\Account\FrontAuth();
            $user_id = parent::create($f_auth , $info,$is_md5);
        }catch(\Exception $e){
            if($e->getCode()=='23000')
                AuctionException::make_throw('200005');
            else
               throw $e ;
        }    	
        return $user_id;
    }
    
    /**
    * 删除用户
    * @param string user_name 用户名
    * @return bool
    */
    public static function remove($user_name){
        $f_auth = new \AuctionCore\Account\FrontAuth();
        $ok = parent::remove($f_auth , $user_name);
        return $ok;
    }


    /**
      * 检查权限
      * @param rule_names string|array  需要验证的规则列表,支持逗号分隔的权限规则或索引数组
      * @return boolean           通过验证返回true;失败返回false
    */
    public function check_auth($rule_names){       
        $user_info = $this->user_info();
        $uid = $user_info['uid'];
        $ok = $this->auth_object->check($rule_names,$uid);
        return $ok;
    }
    
    /**
     * 设置车商用户关系
     */
    public function set_dealer_user($dealer_id)
    {
        $user_info = $this->user_info();
        $uid = $user_info['uid'];
        if(empty($dealer_id)){
            return false;
        }
        $data = array(
            'uid' => $uid,
            'dealer_id' => $dealer_id,
            'status' => 1
        );  
        return  M('dealer_user')->add($data);
    }
    
    /**
     * 根据UID获取账号详情
     */
    static  public function update_by_uid($data){
            $data['passwd'] = md5($data['passwd']);
            $res = M('user')->where("uid='".$data['uid']."'")->save($data);
            return $res;
    }



     //检查access_token的合法性
    public static function check_access_token($uid,$token) {
            $auth = C('auth');
            $token = think_decrypt($token,$auth['key'].$uid);
            if($token) {
                $tarr = explode('O_O',$token);
                if(count($tarr) == 2) {
                    $date = $tarr[0];
                    $md5_key = $tarr[1];
                    if((time()-strtotime($date)) < $auth['timeout']) {
                        $md5_me =  md5(md5($date.$auth['token']));
                        if($md5_key == $md5_me) {
                            return 0;
                        }
                    } else {
                        AuctionException::make_throw('100067');                         
                    }
                }
            }
            AuctionException::make_throw('100067'); 
    }

    //生成访问token
    public static function create_access_token($uid) {
        $auth = C('auth');
        $date = date('Y-m-d H:i:s');
        $token_md5 = md5(md5($date.$auth['token']));
        $token = think_encrypt($date.'O_O'.$token_md5,$auth['key'].$uid);
        return $token;
    }

    /**
     * 设置用户备注
     * @param $uid
     * @param $remark_id
     * @param $remark
     * @param int $type
     * @return bool|mixed
     */
    public static function set_remark($uid,$remark_id,$remark,$type)
    {
        if($uid<1 || $remark_id<1 || empty($remark) || $type<1){
            return false;
        }
        $data = array(
            'uid' => $uid,
            'remark_id'=>$remark_id,
            'remark_type'=>$type,
            'remark'=>$remark,
            'create_time'=> date("Y-m-d H:i:s")
        );
        return M('user_remark')->add($data,array('remark'),true);
    }

    /**
     * //根据手机号判断账号是否已存在
     * @param $mobile
     * @return mixed
     */
    public static function is_user_exists($mobile){
        return  M('user')->where(array('pwd_mobile'=>array('eq',$mobile),'status'=>array('eq',0)))->find();
    }

    /**
     * @param $uid
     * @return bool|mixed
     */
    public static function get_user_by_uid($uid)
    {
        if($uid<1){
            return false;
        }
        return M("user")->where("uid=$uid")->find();
    }
}
